package com.dshore.messagecenter.service.warning.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dshore.messagecenter.dao.warning.MrWarningProcessingDao;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.model.system.SysDict;
import com.dshore.messagecenter.model.topicqueue.MdTopic;
import com.dshore.messagecenter.model.warning.MrWarningProcessing;
import com.dshore.messagecenter.service.warning.MrWarningProcessingService;
import com.dshore.messagecenter.util.Page;


@Service
public class MrWarningProcessingServiceImpl implements MrWarningProcessingService{
	@Autowired
	private MrWarningProcessingDao mrWarningProcessingDao;

	@Override
	public Page<MrWarningProcessing> getPage(MrWarningProcessing mrWarningProcessing) {
		List<MrWarningProcessing>rows=mrWarningProcessingDao.getPage(mrWarningProcessing);
		if(rows!=null&&!rows.isEmpty()) {
			for(MrWarningProcessing warning:rows) {
				warning.setActualNumerical(warning.getActualNumerical()+warning.getUnit());
				Map<String,Object> ws=mrWarningProcessingDao.selectShow(warning);
				if(ws!=null) {
					warning.setAlarmItem((String) ws.get("kpiName"));
					warning.setComparisonOperators((String)ws.get("symbol")+warning.getWarningThreshold()+warning.getUnit());
					warning.setObjType((String)ws.get("objtype"));
					warning.setObj((String)ws.get("obj"));
					warning.setPrincipal((String)ws.get("people"));
					warning.setStatus((String)ws.get("status"));
				}
			}
		}
		long total=mrWarningProcessingDao.getPageCount(mrWarningProcessing);
		Page<MrWarningProcessing>page=new Page<>(mrWarningProcessing.getLimit(), total, mrWarningProcessing.getOffset(), rows);
		return page;
	}

	@Override
	public ResponseResult<Void> save(MrWarningProcessing mrWarningProcessing) {
		ResponseResult<Void>responseResult=new ResponseResult<>();
		try {
			responseResult.setState(400);
			responseResult.setMessage("传入数据不正确");
			String dealPeople=mrWarningProcessing.getDealPeople();
			String dealWay=mrWarningProcessing.getDealWay();
			if(dealPeople!=null&&dealWay!=null) {
				if(!StringUtils.isEmpty(dealPeople.trim())&&!StringUtils.isEmpty(dealWay.trim())) {
					responseResult.setState(200);
					responseResult.setMessage("ok");
					Integer i=mrWarningProcessingDao.save(mrWarningProcessing);
					if(i<=0) {
						responseResult.setState(400);
						responseResult.setMessage("失败");
					}
				}
			}
			
			return responseResult;
		}catch (Exception e) {
			responseResult.setState(500);
			responseResult.setMessage("未知错误");
			return responseResult;
		}
		
		
	}

	@Override
	public List<SysDict> getAlarmItem() {
		return mrWarningProcessingDao.getAlarmItem();
	}

	@Override
	public List<MdTopic> getTopic() {
		return mrWarningProcessingDao.getTopic();
	}

	@Override
	public List<SysDict> getObjType() {
		List<SysDict>list=mrWarningProcessingDao.getObjType();
		return list;
	}

}
